package com.yunji.base.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yunji.base.dao.IFeedbackDao;
import com.yunji.base.domain.FeedbackDO;
import com.yunji.base.domain.MalfunctionDO;
import com.yunji.base.mapper.IFeedbackMapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.model.params.FeedbackParam;
import com.yunji.common.model.vo.FeedbackExportVO;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.message.dao.impl
 * @Title: FeedbackDaoImol
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-10-30 09:32:27      V1.0        HuaAo       新建类
 */
@Repository
public class FeedbackDaoImpl extends BaseDaoImpl<IFeedbackMapper, FeedbackDO> implements IFeedbackDao {

    @Override
    public List<FeedbackDO> searchList(FeedbackParam param) {
        return pageList(param, new LambdaQueryWrapper<FeedbackDO>()
                .like(BeanUtils.isNotNull(param.getSiteName()), FeedbackDO::getSiteName, param.getSiteName())
                .like(BeanUtils.isNotNull(param.getSiteTel()), FeedbackDO::getSiteTel, param.getSiteTel())
                .like(BeanUtils.isNotNull(param.getAgentName()), FeedbackDO::getAgentName, param.getAgentName())
                .in(BeanUtils.isNotNull(param.getMalfunctionIds()), FeedbackDO::getMalfunctionId, param.getMalfunctionIds())
                .in(BeanUtils.isNotNull(param.getFeedbackStatus()), FeedbackDO::getFeedbackStatus, param.getFeedbackStatus())
                .orderByDesc(FeedbackDO::getCreateTime));
    }

    @Override
    public List<FeedbackExportVO> search(FeedbackParam param) {
        MPJLambdaWrapper<FeedbackDO> wrapper = new MPJLambdaWrapper<>();
        return selectJoinList(FeedbackExportVO.class, wrapper.selectAll(FeedbackDO.class)
                .select(MalfunctionDO::getMalfunctionName)
                .leftJoin(MalfunctionDO.class, MalfunctionDO::getMalfunctionId, FeedbackDO::getMalfunctionId)
                .like(BeanUtils.isNotNull(param.getSiteName()), FeedbackDO::getSiteName, param.getSiteName())
                .like(BeanUtils.isNotNull(param.getSiteTel()), FeedbackDO::getSiteTel, param.getSiteTel())
                .like(BeanUtils.isNotNull(param.getAgentName()), FeedbackDO::getAgentName, param.getAgentName())
                .eq(BeanUtils.isNotNull(param.getMalfunctionId()), FeedbackDO::getMalfunctionId, param.getMalfunctionId())
                .orderByDesc(FeedbackDO::getCreateTime));

    }

    @Override
    public List<String> searchTableField(FeedbackParam param) {
//        List<Feedback> list = pageList(param, new QueryWrapper<Feedback>().select(param.getSearchField()).lambda().like(Feedback::getSiteName, param.getSiteName()));
//        return list.stream().map(Feedback::getSiteName).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<FeedbackDO> wrapper = new QueryWrapper<FeedbackDO>()
                .select(param.getSearchField()).lambda()
                .like(BeanUtils.isNotNull(param.getSiteName()), FeedbackDO::getSiteName, param.getSiteName())
                .like(BeanUtils.isNotNull(param.getSiteTel()), FeedbackDO::getSiteTel, param.getSiteTel())
                .like(BeanUtils.isNotNull(param.getAgentName()), FeedbackDO::getAgentName, param.getAgentName())
                .last("limit 5");
        return listObjs(wrapper, Object::toString).stream().distinct().collect(Collectors.toList());
    }

}